<template>
  <div>
    <header>
      <el-button type="success" @click="openCreateDrawer">添加</el-button>
    </header>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="编号" width="180" prop="id"></el-table-column>
      <el-table-column label="名字" width="180" prop="name"></el-table-column>

      <el-table-column label="备注" width="180" prop="remark"></el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="openEditDrawer(scope.row)">修改</el-button>
          <el-button slot="reference" size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer title="新建角色" :visible.sync="createDrawerVisable" direction="rtl">
      <el-form :model="createForm" label-width="100px">
        <el-form-item label="名字">
          <el-input v-model="createForm.name" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="createForm.remark" />
        </el-form-item>
        <!-- <el-form-item label="父角色">
          <el-select v-model="editForm.parentId" placeholder="请选择">
            <el-option
              v-for="(item, index) in parentIdOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="子权限">
          <el-tree show-checkbox highlight-current node-key="value" :props="{ children: 'childrenList', label: 'permissionName' }"
            :data="permissionTree" ref="treeCreate" />
        </el-form-item>

        <el-form-item>
          <el-button size="small" type="primary" @click="handleCreate">确定</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <el-drawer title="修改角色" :visible.sync="editDrawerVisable" direction="rtl">
      <el-form :model="editForm" label-width="100px">
        <el-form-item label="名字">
          <el-input v-model="editForm.name" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.remark" />
        </el-form-item>
        <!-- <el-form-item label="父角色">
          <el-select v-model="editForm.parentId" placeholder="请选择">
            <el-option
              v-for="(item, index) in parentIdOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->

        <el-form-item label="子权限">
          <el-tree show-checkbox highlight-current node-key="value" :props="{ children: 'childrenList', label: 'permissionName' }"
            :data="permissionTree" ref="treeEdit" />
        </el-form-item>

        <el-form-item>
          <el-button size="small" type="primary" @click="handleEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
  import {
    getRoles,
    deleteRoles,
    createRole,
    updateRole,
    getPermissionsAsTree,
  } from '../../api'

  export default {
    name: 'Role',
    components: {},
    data: () => ({
      tableData: [],
      createForm: {
        name: '',
        remark: '',
        authDataList: [],
      },
      editForm: {
        id: '',
        name: '',
        remark: '',
        authDataList: [],
      },
      permissionTree: [],
      createDrawerVisable: false,
      editDrawerVisable: false,
    }),
    created() {
      this.getData()
    },
    methods: {
      getData(input = {
        pageNum: 1,
        pageSize: 5
      }) {
        getRoles(input).then(res => {
          this.tableData = res.data.records
        })
      },
      async openCreateDrawer() {
        this.createDrawerVisable = true
        this.permissionTree = (await getPermissionsAsTree()).data
      },
      async openEditDrawer({
        id,
        name,
        remark,
        authData
      }) {
        this.editDrawerVisable = true

        const authDataList = JSON.parse(authData).authList
        this.editForm.remark = remark
        this.editForm.name = name
        this.editForm.id = id
        this.permissionTree = (await getPermissionsAsTree()).data
        this.$refs.treeEdit.setCheckedKeys(authDataList)


        // const permissions = (await getPermissions()).data
        //   .filter(v => v.id !== id)
        //   .map(v => ({
        //     value: v.permissionName,
        //     lable: v.id,
        //   }))
        //   .concat({ value: 0, label: '无' })

        // this.parentIdOptions = permissions
      },
      handleCreate() {
        this.createForm.authDataList = this.$refs.treeCreate.getCheckedKeys()

        createRole(this.createForm).then(res => {
          if (res.code === 200) {
            this.getData()
            this.createDrawerVisable = false
          }
        })
      },
      handleEdit() {
        this.editForm.authDataList = this.$refs.treeEdit.getCheckedKeys()
        updateRole(this.editForm).then(res => {
          if (res.code === 200) {
            this.getData()
            this.editDrawerVisable = false
          }
        })
      },
      handleDelete(id) {
        this.$confirm('确定删除?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
          .then(() => {
            deleteRoles([id]).then(res => {
              if (res.code === 200) {
                this.getData()
              }
            })
          })
          .catch(() => {})
      },
    },
  }
</script>

<style lang="less" scoped>
</style>
